﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace ZFB.Models.Migrations
{
    /// <inheritdoc />
    public partial class ver10 : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "T_Attachments",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    IconName = table.Column<string>(type: "varchar(max)", unicode: false, nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Attachments", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_Cities",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Cities", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_IdNames",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    TypeName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_IdNames", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_Permissions",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Permissions", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_Roles",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Roles", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_Settings",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Value = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Settings", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "T_AdminUsers",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    PhoneNum = table.Column<string>(type: "varchar(max)", unicode: false, nullable: false),
                    PasswordHash = table.Column<string>(type: "varchar(max)", unicode: false, nullable: false),
                    PasswordSalt = table.Column<string>(type: "varchar(max)", unicode: false, nullable: false),
                    Email = table.Column<string>(type: "varchar(max)", unicode: false, nullable: false),
                    CityId = table.Column<long>(type: "bigint", nullable: true),
                    LoginErrorTimes = table.Column<int>(type: "int", nullable: false),
                    LastLoginErrorDateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_AdminUsers", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_AdminUsers_T_Cities_CityId",
                        column: x => x.CityId,
                        principalTable: "T_Cities",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_Regions",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CityId = table.Column<long>(type: "bigint", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Regions", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_Regions_T_Cities_CityId",
                        column: x => x.CityId,
                        principalTable: "T_Cities",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_Users",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    PhoneNum = table.Column<string>(type: "varchar(max)", unicode: false, nullable: false),
                    PasswordHash = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    PasswordSalt = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    LoginErrorTimes = table.Column<int>(type: "int", nullable: false),
                    LastLoginErrorDateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
                    CityId = table.Column<long>(type: "bigint", nullable: true),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Users", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_Users_T_Cities_CityId",
                        column: x => x.CityId,
                        principalTable: "T_Cities",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_RolePermissions",
                columns: table => new
                {
                    T_PermissionsId = table.Column<long>(type: "bigint", nullable: false),
                    T_RolesId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_RolePermissions", x => new { x.T_PermissionsId, x.T_RolesId });
                    table.ForeignKey(
                        name: "FK_T_RolePermissions_T_Permissions_T_PermissionsId",
                        column: x => x.T_PermissionsId,
                        principalTable: "T_Permissions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_T_RolePermissions_T_Roles_T_RolesId",
                        column: x => x.T_RolesId,
                        principalTable: "T_Roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "T_AdminLogs",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    AdminUserId = table.Column<long>(type: "bigint", nullable: false),
                    Message = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_AdminLogs", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_AdminLogs_T_AdminUsers_AdminUserId",
                        column: x => x.AdminUserId,
                        principalTable: "T_AdminUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_AdminUserRoles",
                columns: table => new
                {
                    T_AdminUsersId = table.Column<long>(type: "bigint", nullable: false),
                    T_RolesId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_AdminUserRoles", x => new { x.T_AdminUsersId, x.T_RolesId });
                    table.ForeignKey(
                        name: "FK_T_AdminUserRoles_T_AdminUsers_T_AdminUsersId",
                        column: x => x.T_AdminUsersId,
                        principalTable: "T_AdminUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_T_AdminUserRoles_T_Roles_T_RolesId",
                        column: x => x.T_RolesId,
                        principalTable: "T_Roles",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "T_Communities",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    RegionId = table.Column<long>(type: "bigint", nullable: false),
                    Location = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Traffic = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    BuiltYear = table.Column<int>(type: "int", nullable: true),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Communities", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_Communities_T_Regions_RegionId",
                        column: x => x.RegionId,
                        principalTable: "T_Regions",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_Houses",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    CommunityId = table.Column<long>(type: "bigint", nullable: false),
                    RoomTypeId = table.Column<long>(type: "bigint", nullable: false),
                    Address = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    MonthRent = table.Column<int>(type: "int", nullable: false),
                    StatusId = table.Column<long>(type: "bigint", nullable: false),
                    Area = table.Column<decimal>(type: "decimal(18,2)", nullable: false),
                    DecorateStatusId = table.Column<long>(type: "bigint", nullable: false),
                    TotalFloorCount = table.Column<int>(type: "int", nullable: false),
                    FloorIndex = table.Column<int>(type: "int", nullable: false),
                    TypeId = table.Column<long>(type: "bigint", nullable: false),
                    Direction = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    LookableDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    CheckInDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    OwnerName = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    OwnerPhoneNum = table.Column<string>(type: "varchar(max)", unicode: false, nullable: false),
                    Description = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_Houses", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_Houses_T_Communities_CommunityId",
                        column: x => x.CommunityId,
                        principalTable: "T_Communities",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_Houses_T_IdNames_DecorateStatusId",
                        column: x => x.DecorateStatusId,
                        principalTable: "T_IdNames",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_Houses_T_IdNames_RoomTypeId",
                        column: x => x.RoomTypeId,
                        principalTable: "T_IdNames",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_Houses_T_IdNames_StatusId",
                        column: x => x.StatusId,
                        principalTable: "T_IdNames",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_Houses_T_IdNames_TypeId",
                        column: x => x.TypeId,
                        principalTable: "T_IdNames",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_HouseAppointments",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    UserId = table.Column<long>(type: "bigint", nullable: true),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    PhoneNum = table.Column<string>(type: "varchar(max)", unicode: false, nullable: false),
                    VisitDate = table.Column<DateTime>(type: "datetime2", nullable: false),
                    HouseId = table.Column<long>(type: "bigint", nullable: false),
                    Status = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    FollowAdminUserId = table.Column<long>(type: "bigint", nullable: true),
                    FollowDateTime = table.Column<DateTime>(type: "datetime2", nullable: true),
                    RowVersion = table.Column<byte[]>(type: "varbinary(max)", fixedLength: true, nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_HouseAppointments", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_HouseAppointments_T_AdminUsers_FollowAdminUserId",
                        column: x => x.FollowAdminUserId,
                        principalTable: "T_AdminUsers",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_HouseAppointments_T_Houses_HouseId",
                        column: x => x.HouseId,
                        principalTable: "T_Houses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_T_HouseAppointments_T_Users_UserId",
                        column: x => x.UserId,
                        principalTable: "T_Users",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "T_HouseAttachments",
                columns: table => new
                {
                    T_AttachmentsId = table.Column<long>(type: "bigint", nullable: false),
                    T_HousesId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_HouseAttachments", x => new { x.T_AttachmentsId, x.T_HousesId });
                    table.ForeignKey(
                        name: "FK_T_HouseAttachments_T_Attachments_T_AttachmentsId",
                        column: x => x.T_AttachmentsId,
                        principalTable: "T_Attachments",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_T_HouseAttachments_T_Houses_T_HousesId",
                        column: x => x.T_HousesId,
                        principalTable: "T_Houses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "T_HousePics",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    HouseId = table.Column<long>(type: "bigint", nullable: false),
                    Url = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    ThumbUrl = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    CreateDateTime = table.Column<DateTime>(type: "datetime2", nullable: false),
                    IsDeleted = table.Column<bool>(type: "bit", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_T_HousePics", x => x.Id);
                    table.ForeignKey(
                        name: "FK_T_HousePics_T_Houses_HouseId",
                        column: x => x.HouseId,
                        principalTable: "T_Houses",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_T_AdminLogs_AdminUserId",
                table: "T_AdminLogs",
                column: "AdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_T_AdminUserRoles_T_RolesId",
                table: "T_AdminUserRoles",
                column: "T_RolesId");

            migrationBuilder.CreateIndex(
                name: "IX_T_AdminUsers_CityId",
                table: "T_AdminUsers",
                column: "CityId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Communities_RegionId",
                table: "T_Communities",
                column: "RegionId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAppointments_FollowAdminUserId",
                table: "T_HouseAppointments",
                column: "FollowAdminUserId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAppointments_HouseId",
                table: "T_HouseAppointments",
                column: "HouseId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAppointments_UserId",
                table: "T_HouseAppointments",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HouseAttachments_T_HousesId",
                table: "T_HouseAttachments",
                column: "T_HousesId");

            migrationBuilder.CreateIndex(
                name: "IX_T_HousePics_HouseId",
                table: "T_HousePics",
                column: "HouseId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Houses_CommunityId",
                table: "T_Houses",
                column: "CommunityId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Houses_DecorateStatusId",
                table: "T_Houses",
                column: "DecorateStatusId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Houses_RoomTypeId",
                table: "T_Houses",
                column: "RoomTypeId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Houses_StatusId",
                table: "T_Houses",
                column: "StatusId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Houses_TypeId",
                table: "T_Houses",
                column: "TypeId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Regions_CityId",
                table: "T_Regions",
                column: "CityId");

            migrationBuilder.CreateIndex(
                name: "IX_T_RolePermissions_T_RolesId",
                table: "T_RolePermissions",
                column: "T_RolesId");

            migrationBuilder.CreateIndex(
                name: "IX_T_Users_CityId",
                table: "T_Users",
                column: "CityId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "T_AdminLogs");

            migrationBuilder.DropTable(
                name: "T_AdminUserRoles");

            migrationBuilder.DropTable(
                name: "T_HouseAppointments");

            migrationBuilder.DropTable(
                name: "T_HouseAttachments");

            migrationBuilder.DropTable(
                name: "T_HousePics");

            migrationBuilder.DropTable(
                name: "T_RolePermissions");

            migrationBuilder.DropTable(
                name: "T_Settings");

            migrationBuilder.DropTable(
                name: "T_AdminUsers");

            migrationBuilder.DropTable(
                name: "T_Users");

            migrationBuilder.DropTable(
                name: "T_Attachments");

            migrationBuilder.DropTable(
                name: "T_Houses");

            migrationBuilder.DropTable(
                name: "T_Permissions");

            migrationBuilder.DropTable(
                name: "T_Roles");

            migrationBuilder.DropTable(
                name: "T_Communities");

            migrationBuilder.DropTable(
                name: "T_IdNames");

            migrationBuilder.DropTable(
                name: "T_Regions");

            migrationBuilder.DropTable(
                name: "T_Cities");
        }
    }
}
